package com.hundredapp.oa.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.domain.OaUser;
import com.hundredapp.oa.repository.UserManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Login extends ActionSupport {

	@Autowired
	private UserManager userManager;

	private String username;
	private String password;
	private String prePage;
	private String errorinfo = "";

	public String execute() {
		if (username == null && password == null) {
			errorinfo = "";
			return INPUT;
		}
		if (username.isEmpty() || password.isEmpty()) {
			errorinfo = "请输入账号名和密码";
		} else {
			OaUser login_user = userManager.login(username, password.replaceAll("'", ""));
			if (login_user != null) {
				if (login_user.getStatus() != 1) {
					errorinfo = "该账号已被禁用，请联系管理员。";
				} else {
					Map<String, Object> session_map = ActionContext
							.getContext().getSession();
					session_map.put("lgoin_user_id", login_user.getId());
					Object obj = session_map.get("prePage");
					if (obj != null) {
						prePage = obj.toString();
					} else {
						prePage = "index";
					}
					session_map.put("prePage", null);
					return SUCCESS;
				}
			} else {
				errorinfo = "账号或者密码错误，请重试。";
			}
		}
		return INPUT;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPrePage() {
		return prePage;
	}

	public void setPrePage(String prePage) {
		this.prePage = prePage;
	}

	public String getErrorinfo() {
		return errorinfo;
	}
}
